package com.richapp.launcher.service;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.view.Gravity;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.richapp.launcher.busevent.EventCameraMove;
import com.richapp.launcher.database.system.dao.BoxModelDao;
import com.richapp.launcher.database.system.dao.SystemValueModelDao;
import com.richapp.launcher.net.camera.AnyanUdpServer;
import com.richapp.launcher.net.message.MsgConstant;
import com.richapp.launcher.net.remote.RemoteClient;
import com.richapp.launcher.serialport.gateway.GatewaytConstant;

import java.util.HashMap;
import java.util.Map;

import de.greenrobot.event.EventBus;

/**
 * Created by jack on 16/3/4.
 */
public class CameraService extends Service {

    private AnyanUdpServer anyanUdpServer = null;

    private EventCameraMove lastEventCameraMove = null;

    private int moveCount = 0;

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        return START_STICKY;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        EventBus.getDefault().register(this);

        anyanUdpServer = AnyanUdpServer.getInstance();
        anyanUdpServer.setContext(this);

        new Thread(new Runnable() {
            @Override
            public void run() {
                anyanUdpServer.start();
            }
        }).start();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        anyanUdpServer.stop();
    }

    public void onEventMainThread(EventCameraMove eventCameraMove){

        String alarmFlag = new SystemValueModelDao(this).getByName(new BoxModelDao(this).getCurrentBox().getBoxCpuId(), GatewaytConstant.SYSTEM_KEY_NAME_ALARM_FLAG).getValue();
        if("true".equals(alarmFlag)){
            Toast toast = Toast.makeText(CameraService.this,"移动监测提醒||摄像头"+eventCameraMove.getCameraId(),Toast.LENGTH_LONG);
            toast.setGravity(Gravity.CENTER,0,0);
            toast.show();
        }

        moveCount++;

        //进行计数和时间间隔处理
        if(lastEventCameraMove == null){
            lastEventCameraMove = eventCameraMove;
        }else{
            if((eventCameraMove.getMoveTime() - lastEventCameraMove.getMoveTime()) > 10*60*1000){
                lastEventCameraMove = eventCameraMove;
                moveCount = 1;
            }else{
                if(moveCount == 2){ //如果15分种内收到2条，则进行处理
                    if("true".equals(alarmFlag)){
                        Map<String,Object> map = new HashMap<String,Object>();
                        map.put("type", MsgConstant.MSG_TYPE_CAMERA_MOVING);
                        map.put("currentBoxCpuId",new BoxModelDao(this).getCurrentBox().getBoxCpuId());
                        map.put("time",eventCameraMove.getMoveTime());
                        map.put("cameraId",eventCameraMove.getCameraId());
                        String message = JSON.toJSONString(map);
                        //发送到服务器
                        RemoteClient.getInstance().sendMessage(message);
                    }
                }
            }
        }
    }
}
